package com.business_clean.app.config;

import com.alibaba.fastjson.JSON;
import com.business_clean.app.App;
import com.business_clean.app.callback.OnNationListener;
import com.business_clean.app.callback.ProviceCityAreaDataCallBack;
import com.business_clean.app.network.NetUrl;
import com.business_clean.app.uitl.MMKVHelper;
import com.business_clean.data.initconfig.city.ProviceCityAreaListData;
import com.business_clean.data.initconfig.NationEntity;
import com.business_clean.data.mode.init.InitDataEntity;
import com.business_clean.data.mode.login.UserInfo;
import com.business_clean.data.mode.project.ProjectManager;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.functions.Consumer;
import me.hgj.mvvmhelper.ext.LogExtKt;
import rxhttp.wrapper.param.RxHttp;

/**
 * 需要在后台 请求的操作
 */
public class AsyncRequestUtil {

    /**
     * 请求城市列表
     *
     * @param callBack
     */
    public static void requestProviceCityArea(final ProviceCityAreaDataCallBack callBack) {
        RxHttp.get(NetUrl.INIT_CITY)
                .asResponse(ProviceCityAreaListData.class)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<ProviceCityAreaListData>() {
                    @Override
                    public void accept(ProviceCityAreaListData list) throws Throwable {
                        MMKVHelper.putString(ConstantMMVK.PROVICE_CITY_AREA_DATA, JSON.toJSONString(list.getList()));
                        if (callBack != null) {
                            callBack.cityDataCallBack(list.getList());
                        }
                    }
                });
    }

    /**
     * 请求源数据
     */
    public static void requestInitData() {
        RxHttp.get(NetUrl.GET_INIT_DATA)
                .asResponse(InitDataEntity.class)
                .subscribe(new Consumer<InitDataEntity>() {
                    @Override
                    public void accept(InitDataEntity initDataEntity) throws Throwable {
                        LogExtKt.logI("请求回来的源数据", "");
                        MMKVHelper.putString(ConstantMMVK.INIT_DATA, JSON.toJSONString(initDataEntity));
                        MMKVHelper.putString(ConstantMMVK.IN_CLASS_TIME_LIST, JSON.toJSONString(initDataEntity.getIn_class_time_list()));
                        MMKVHelper.putString(ConstantMMVK.OUT_CLASS_TIME_LIST, JSON.toJSONString(initDataEntity.getOut_class_time_list()));
                    }
                });
    }


    /**
     * 通知后台切换了接口
     */
    public static void requestChangeProject(String uuid) {
        RxHttp.get(NetUrl.CHANGE_PROJECT)
                .add("project_uuid", uuid)
                .asResponse(Object.class)
                .subscribe();
    }

    /**
     * 后台更新个人信息
     */
    public static void requestUserInfoData() {
        RxHttp.get(NetUrl.GET_USER_INFO)
                .asResponse(UserInfo.class)
                .subscribe(new Consumer<UserInfo>() {
                    @Override
                    public void accept(UserInfo userInfo) throws Throwable {
                        MMKVHelper.putString(ConstantMMVK.USER_INFO, JSON.toJSONString(userInfo));
                        App.appViewModelInstance.getProjectInfo().setValue(userInfo.getProject());
                        App.appViewModelInstance.getUserInfo().setValue(userInfo);
                    }
                });
    }


    /**
     * 请求民族列表
     */
    public static void requestNationData(OnNationListener listener) {
        RxHttp.get(NetUrl.GET_NATION_LIST)
                .asResponse(NationEntity.class)
                .subscribe(new Consumer<NationEntity>() {
                    @Override
                    public void accept(NationEntity data) throws Throwable {
                        MMKVHelper.putString(ConstantMMVK.NATION_LIST, JSON.toJSONString(data.getList()));
                        if (listener != null) {
                            listener.nationCallBack(data.getList());
                        }
                    }
                });
    }


    /**
     * 更新所有项目
     */
    public static void requestProjectAll() {
        RxHttp.get(NetUrl.GET_PROJECT_MANGER_ALL)
                .asResponse(ProjectManager.class)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<ProjectManager>() {
                    @Override
                    public void accept(ProjectManager data) throws Throwable {
                        App.appViewModelInstance.getAllProjectManager().setValue(data);
                    }
                });
    }
}
